package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.dto.PermissionListDTO;
import com.dashu.core.dto.RelationshipListDTO;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.PermissionDTO;
import com.dashu.core.service.PermissionService;
import com.dashu.core.service.UserService;
import com.dashu.core.vo.PermissionListVO;
import com.dashu.core.vo.PermissionVO;
import com.dashu.core.vo.UpdateEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode(ReplRyCode.SUCCESS.getCode(/ReplRyCode.SUCCESS.getCode(7 ReplRyCode.SUCCESS.getCode(5:2ReplRyCode.SUCCESS.getCode(
 */

@RequestMapping("dashu/permission")
@RestController
@Slf4j
@Api(value = "dashu/permission", tags = {"角色功能权限模块"}, description = "后台")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserService userService;

    @PostMapping("/addRule")
    @ApiOperation(value = "角色功能权限添加", notes = "角色功能权限添加")
    public ResultBean<PermissionDTO> addRule(@RequestBody @Validated PermissionVO permissionVO) throws Exception {
        PermissionDTO permissionDTO = permissionService.existByPermissionRuleName(permissionVO.getRuleName());
        if (permissionDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_PERMISSION.getCode(), ReplRyCode.EXIST_PERMISSION.getMessage(), null);
        }
        if (permissionVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "角色功能权限添加", permissionService.add(permissionVO));
    }

    @PostMapping("/updataRule")
    @ApiOperation(value = "角色功能权限修改", notes = "角色功能权限修改")
    public ResultBean<PermissionDTO> updataRule(@RequestBody @Validated UpdateEntityVO<PermissionVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        PermissionDTO permissionDTO = permissionService.updataByPermissionRuleId(updateEntityVO);
        if (permissionDTO != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "角色功能权限修改", permissionDTO);
        }
        return new ResultBean<>(ReplRyCode.PERMISSION_NOT_DATA.getCode(), ReplRyCode.PERMISSION_NOT_DATA.getMessage(), permissionDTO);
    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询角色功能权限列表", notes = "父节点查询角色功能权限列表")
    public ResultBean<List<PermissionDTO>> findBase(@RequestBody @Validated PageEnty<Long> baseVo) {
        if (baseVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询角色功能权限列表", permissionService.findByBaseId(baseVo));

    }

    @GetMapping("/findByRule")
    @ApiOperation(value = "角色功能权限查询", notes = "权角色功能限查询")
    public ResultBean<PermissionDTO> findByRule(Long ruleId) {
        if (ruleId != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "角色功能权限查询", permissionService.findByPermissionRuleId(ruleId));
        }
        return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
    }

    @GetMapping("/findByRuleName")
    @ApiOperation(value = "角色功能权限名称查询", notes = "角色功能权限名称查询")
    public ResultBean<PermissionDTO> findName(String ruleName) {
        if (ruleName == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "权功名称查询", permissionService.PermissionRuleName(ruleName));
    }

    @DeleteMapping("/deleteRule")
    @ApiOperation(value = "角色功能权限删除", notes = "角色功能权限删除")
    public ResultBean<Boolean> deleteRule(Long ruleId) {
        if (ruleId == null)
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        if (userService.existByPermissionRuleId(ruleId))
            return new ResultBean<>(ReplRyCode.EXIST_PERMISSION_USER.getCode(), "角色功能权限删除", null);
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "角色功能权限删除", permissionService.deleteByPermissionRuleId(ruleId));

    }

    @PostMapping("/findRuleList")
    @ApiOperation(value = "角色功能权限列表", notes = "角色功能权限列表")
    public ResultBean<PageDTO<PermissionListDTO>> findRuleList(@RequestBody @Validated PageEnty<PermissionListVO> permissionListPageEntyVO) throws Exception {
        PageDTO<PermissionListDTO> pageDTO = new PageDTO<>();
        if (permissionListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = permissionService.permissionCount(permissionListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(permissionService.findByPermissionList(permissionListPageEntyVO));

        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "角色功能权限列表", pageDTO);

    }

}
